project(dbgshim)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CLR_CMAKE_RUNTIME_MONO 1)
set(CLR_DIR ${CLR_REPO_ROOT_DIR}/src/coreclr)
set(CMAKE_OSX_ARCHITECTURES ${CMAKE_SYSTEM_PROCESSOR})
set(CMAKE_EXE_LINKER_FLAGS_CHECKED "")
set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "")
set(CLR_CMAKE_HOST_ARCH ${CMAKE_GENERATOR_PLATFORM})
set(FEATURE_EVENT_TRACE 0)

if(HOST_WIN32)
    if(HOST_X86)
        set(CLR_CMAKE_HOST_ARCH x86)
    elseif(HOST_ARM64)
        set(CLR_CMAKE_HOST_ARCH arm64)
    elseif(HOST_ARM)
        set(CLR_CMAKE_HOST_ARCH arm)
    elseif(HOST_AMD64)
        set(CLR_CMAKE_HOST_ARCH x64)
    endif()
endif()

include(${CLR_ENG_NATIVE_DIR}/configurecompiler.cmake)

add_definitions(-D_WIN32_WINNT=0x0602)

include_directories(
  ${CMAKE_CURRENT_SOURCE_DIR}/../../..
  ${PROJECT_SOURCE_DIR}/../../
  ${PROJECT_SOURCE_DIR}/../mscordbi
  ${PROJECT_SOURCE_DIR}/../mscordbi/socket-dbi
  ${CLR_DIR}/md/enc
  ${CLR_DIR}/inc
  ${CLR_DIR}/pal/inc
  ${CLR_DIR}/md/inc
  ${CLR_DIR}/md/compiler
  ${CLR_DIR}/pal/prebuilt/inc
  ${CLR_DIR}/nativeresources)

if (CLR_CMAKE_HOST_UNIX)
  include_directories(${CLR_DIR}/pal/inc)
  include_directories(${CLR_DIR}/pal/inc/rt)
  include_directories(${CLR_DIR}/pal/src/safecrt)

  append("-Wno-missing-prototypes  -Wno-pointer-arith -Wno-macro-redefined" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)

  include_directories(${CLR_DIR}/pal/inc/rt/cpp)
endif (CLR_CMAKE_HOST_UNIX)

set(DBGSHIM_SOURCES
    dbgshim.cpp
)

if(HOST_DARWIN)
set(OS_LIBS "-framework CoreFoundation" "-framework Foundation")
elseif(HOST_LINUX)
set(OS_LIBS pthread m dl)
elseif(HOST_WIN32)
set(OS_LIBS bcrypt.lib Mswsock.lib ws2_32.lib psapi.lib version.lib advapi32.lib winmm.lib kernel32.lib)
set(DBGSHIM_SOURCES "${DBGSHIM_SOURCES};${VERSION_FILE_RC_PATH}") # this is generated by GenerateMonoVersionFile in mono.proj
endif()

add_library(dbgshim SHARED ${DBGSHIM_SOURCES})

set(DBGSHIM_LIBRARIES
    ${OS_LIBS}
    utilcodestaticnohost
)

if(CLR_CMAKE_HOST_UNIX)
    list(APPEND DBGSHIM_LIBRARIES
        coreclrpal
        palrt
        nativeresourcestring
    )
endif()

target_link_libraries(dbgshim PRIVATE ${DBGSHIM_LIBRARIES} monoapi)
install_with_stripped_symbols(dbgshim TARGETS lib)
